<?php
/**
 * 兼容第三方插件补丁文件
 * @appapi 兼容重整 
 */
function mc_update($mid, $update) {
    model('core/member')->member_update($mid,$update);
    return true;
}
function updateMember($mid, $update = array()) {
    $member = pdo_get('core_members', array('id' => $mid));
    if (empty($member)) {
        if (empty($update['mobile']) && empty($update['email'])) {
            return false;
        }
        $update['uniacid'] = $_SESSION['uniacid'];
        $update['createtime'] = TIMESTAMP;
        pdo_insert('core_members', $update);
        $insert_id = pdo_insertid();
        return $insert_id;
    } else {
        model('core/member')->member_update($mid,$update);
        return true;
    }
}
function mc_fetch($mid, $fields = array()) {
    $result = array();
    if (is_array($mid)) {
        foreach ($mid as $id) {
            $user_info = mc_fetch_one($id);
            if (!empty($user_info) && !empty($fields)) {
                foreach ($fields as $field) {
                    $result[$id][$field] = $user_info[$field];
                }
                $result[$id]['uid'] = $id;
            } else {
                $result[$id] = $user_info;
            }
        }
    } else {
        $user_info = mc_fetch_one($mid);
        if (!empty($user_info) && !empty($fields)) {
            foreach ($fields as $field) {
                $result[$field] = $user_info[$field];
            }
            $result['uid'] = $mid;
        } else {
            $result = $user_info;
        }
    }
    return $result;
}

function mc_fetch_one($mid) {
    $mid = mc_openid2uid($mid);
    $member = pdo_get('core_members', array('id' => $mid));
    if (!empty($member)) {
        $member['avatar'] = tomedia($member['avatar']);
        $member['credit1'] = floatval($member['credit1']);
        $member['credit2'] = floatval($member['credit2']);
        $member['credit3'] = floatval($member['credit3']);
        $member['credit4'] = floatval($member['credit4']);
        $member['credit5'] = floatval($member['credit5']);
        $member['credit6'] = floatval($member['credit6']);
    } else {
        $member = array();
    }
    return $member;
}
function mc_fansinfo($openidOrmid, $acid = 0, $uniacid = 0) {
    if (!$openidOrmid) {
        return array();
    }
	$fans= model('core/core')->get_fans($openidOrmid);
    return $fans;
}
function mc_oauth_fans($openid, $acid = 0){
	$mc_oauth_fans_table = table('mc_oauth_fans');
	if (!empty($acid)) {
		$mc_oauth_fans_table->searchWithAcid($acid);
	}
	$mc_oauth_fans_table->searchWithoAuthopenid($openid);
	$fan = $mc_oauth_fans_table->get();
	return $fan;
}
function mc_oauth_userinfo() {
    global $_W, $_GPC;
    $fans = model('core/core')->get_fans($_SESSION['openid']);
    return $fans;
}
function mc_oauth_account_userinfo($url = '') {
	//首次访问自动获取粉丝信息，无需处理！
	return false;
}
function mc_require($uid, $fields, $pre = '') {
	//首次访问自动获取粉丝信息，无需处理！
	return false;
}
function mc_credit_update($uid, $credittype, $creditval = 0, $log = array()) {
    global $_W;
    $credittype = trim($credittype);
    $clerk_types = array('1' => '线上操作', '2' => '系统后台', '3' => '店员',);
    $creditval = floatval($creditval);
    if (empty($creditval)) {
        return true;
    }
    $value = pdo_getcolumn('core_members', array('id' => $uid), $credittype);
    if ($creditval > 0 || ($value + $creditval >= 0) || $credittype == 'credit6') {
        pdo_update('core_members', array($credittype => $value + $creditval), array('id' => $uid));
    } else {
        return error('-1', "积分类型为“{$credittype}”的积分不够，无法操作。");
    }
    return true;
}
function mc_openid2uid($openid) {
    global $_W;
    if (is_numeric($openid)) {
        return $openid;
    }
    if (is_string($openid)) {
        $fans_info = pdo_get('core_fans', array('uniacid' => $_SESSION['uniacid'], 'openid' => $openid), array('mid'));
        return !empty($fans_info) ? $fans_info['mid'] : false;
    }
    if (is_array($openid)) {
        $uids = array();
        foreach ($openid as $k => $v) {
            if (is_numeric($v)) {
                $uids[] = intval($v);
            } elseif (is_string($v)) {
                $fans[] = istripslashes(str_replace('', '', $v));
            }
        }
        if (!empty($fans)) {
            $fans = pdo_getall('core_fans', array('uniacid' => $_SESSION['uniacid'], 'openid' => $fans), array('mid', 'openid'), 'mid');
            $fans = array_keys($fans);
            $uids = array_merge((array)$uids, $fans);
        }
        return $uids;
    }
    return false;
}
function mc_credit_fetch($uid, $types = array()) {
	if (empty($types) || $types == '*') {
		$select = array('credit1', 'credit2', 'credit3', 'credit4', 'credit5', 'credit6');
	} else {
		$struct = mc_credit_types();
		foreach ($types as $key => $type) {
			if (!in_array($type, $struct)) {
				unset($types[$key]);
			}
		}
		$select = $types;
	}
	return pdo_get('core_members', array('id' => $uid), $select);
}
function mc_credit_types(){
	static $struct = array('credit1','credit2','credit3','credit4','credit5','credit6');
	return $struct;
}
function mc_groups($uniacid = 0) {
	global $_W;
	$uniacid = intval($uniacid);
	if (empty($uniacid)) {
		$uniacid = $_W['uniacid'];
	}
	return pdo_getall('core_member_groups', array('uniacid' => $uniacid), array(), 'level', 'credit');
}
function mc_fans_groups($force_update = false) {
	return true;
}
function _mc_login($member) {
	return false;
}
function mc_fields() {
	return $fields;
}
function mc_acccount_fields($uniacid = 0, $is_available = true) {
	return $fields;
}
function mc_handsel($touid, $fromuid, $handsel, $uniacid = '') {
	return true;
}
function mc_uid2openid($uid) {
	return false;
}
function mc_init_fans_info($openid, $force_init_member = false){
	return true;
}
function mc_show_tag($groupid){
	if ($groupid) {
		$fans_tag = mc_fans_groups();
		$tagid_arr = explode(',', trim($groupid, ','));
		foreach ($tagid_arr as $tagid) {
			$tag_show .= $fans_tag[$tagid]['name'] . ', ';
		}
		$tag_show = rtrim($tag_show, ', ');
	} else {
		$tag_show = '无标签';
	}
	return $tag_show;
}
function mc_card_settings_hide($item = '') {
	return false;
}
function mc_card_grant_credit($openid, $card_fee, $storeid = 0, $modulename) {
	return false;
}
function mc_current_real_uniacid() {
	global $_W;
	if (!empty($_W['account']['link_uniacid']) || (!empty($_W['account']) && $_W['uniacid'] != $_W['account']['uniacid'])) {
		return $_W['account']['uniacid'];
	} else {
		return $_W['uniacid'];
	}
}
function mc_parse_profile($profile) {
	global $_W;
	if (empty($profile)) {
		return array();
	}
	if (!empty($profile['avatar'])) {
		$profile['avatar'] = tomedia($profile['avatar']);
	} else {
		$profile['avatar'] = '../core/resource/images/nopic.jpg';
	}
	$profile['avatarUrl'] = $profile['avatar'];
	$profile['birth'] = array(
		'year' => $profile['birthyear'],
		'month' => $profile['birthmonth'],
		'day' => $profile['birthday'],
	);
	$profile['reside'] = array(
		'province' => $profile['resideprovince'],
		'city' => $profile['city'],
		'district' => $profile['dist']
	);
	if(empty($profile['email']) || (!empty($profile['email']) && substr($profile['email'], -6) == 'we7.cc' && strlen($profile['email']) == 39)) {
		$profile['email'] = '';
	}
	return $profile;
}
function mc_member_export_parse($members, $header = array()){
	if (empty($members)) {
		return false;
	}
	return false;
}
function mc_fans_has_member_info($tag) {
	if (is_base64($tag)) {
		$tag = base64_decode($tag);
	}
	if (is_serialized($tag)) {
		$tag = iunserializer($tag);
	}
	$profile = array();
	if (!empty($tag)) {
		if(!empty($tag['nickname'])) {
			$profile['nickname'] = $tag['nickname'];
		}
		if(!empty($tag['sex'])) {
			$profile['gender'] =$tag['sex'];
		}
		if(!empty($tag['province'])) {
			$profile['resideprovince'] = $tag['province'];
		}
		if(!empty($tag['city'])) {
			$profile['residecity'] = $tag['city'];
		}
		if(!empty($tag['country'])) {
			$profile['nationality'] = $tag['country'];
		}
		if(!empty($tag['headimgurl'])) {
			$profile['avatar'] = rtrim($tag['headimgurl']);
		}
	}
	return $profile;
}
function mc_fans_chats_record_formate($chat_record) {
	return $chat_record;
}
function mc_send_content_formate($data) {
	return array(
		'send' => $send,
		'content' => $content
	);
}
function mc_fans_tag_fields() {
	return model('core/fans')->get_tag_fields();
}
